package com.tebiecloud.goods.server.controller.fegin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.goods.client.model.entity.Goods;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import com.tebiecloud.goods.client.model.vo.GoodsSpecificationVO;
import com.tebiecloud.goods.client.service.IGoodsSpecificationServiceClient;
import com.tebiecloud.goods.server.service.GoodsService;
import com.tebiecloud.goods.server.service.GoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/feign/specification")
public class SpecificationController implements IGoodsSpecificationServiceClient {

    @Autowired
    GoodsSpecificationService goodsSpecificationService;

    @Autowired
    GoodsService goodsService;

    /**
     * 获取规格信息列表
     * @param listGoodsSpecificationId
     * @return
     */
    @PostMapping("listGoodsSpecificationVO")
    @Override
    public ResultBody<List<GoodsSpecificationVO>> listGoodsSpecificationVO(List<Long> listGoodsSpecificationId) {
        List<GoodsSpecificationVO> goodsSpecificationVOS = new ArrayList<>();
        //获取商品规格列表
        QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<>();
        goodsSpecificationQueryWrapper.in("spec_id",listGoodsSpecificationId);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationService.list(goodsSpecificationQueryWrapper);
        if(goodsSpecifications!=null && goodsSpecifications.size()>0){
            for(GoodsSpecification goodsSpecification:goodsSpecifications){
                GoodsSpecificationVO goodsSpecificationVO = new GoodsSpecificationVO();
                //查询商品信息
                QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
                goodsQueryWrapper.eq("goods_id",goodsSpecification.getGoodsId());
                Goods goods = goodsService.getOne(goodsQueryWrapper);
                if(goods!=null){
                    goodsSpecificationVO.setStoreId(goods.getStoreId());
                    goodsSpecificationVO.setGoodsName(goods.getName());
                    String goodsPic = "";
                    if(StringUtils.isNotEmpty(goods.getCarouselPics())){
                        goodsPic = goods.getCarouselPics().split(",")[0];
                    }else if(StringUtils.isNotEmpty(goods.getDetailPics())){
                        goodsPic = goods.getDetailPics().split(",")[0];
                    }
                    goodsSpecificationVO.setGoodsPic(goodsPic);
                }
                goodsSpecificationVO.setName(goodsSpecification.getName());
                goodsSpecificationVO.setPrice(goodsSpecification.getPrice());
                goodsSpecificationVO.setGoodsId(goodsSpecification.getGoodsId());
                goodsSpecificationVO.setStockQty(goodsSpecification.getStockQty());
                goodsSpecificationVOS.add(goodsSpecificationVO);
            }
        }
        return ResultBodyUtils.ok(goodsSpecificationVOS);
    }

    @PostMapping("infoGoodsSpecification")
    @Override
    public ResultBody<GoodsSpecification> infoGoodsSpecification(long specId) {
        QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<>();
        goodsSpecificationQueryWrapper.eq("spec_id",specId);
        GoodsSpecification goodsSpecificationList = goodsSpecificationService.getOne(goodsSpecificationQueryWrapper);
        return ResultBodyUtils.ok(goodsSpecificationList);
    }

    @PostMapping("updateStockQty")
    @Override
    public ResultBody updateStockQty(@RequestParam("goodsSpecificationId") long goodsSpecificationId,
                                     @RequestParam("amount") int amount) {
        Map<String,String> resultMap = goodsSpecificationService.updateStockQty(goodsSpecificationId,amount);
        if(resultMap.get("status").equals("error")){
            return ResultBodyUtils.failed(com.tebiecloud.act.client.constants.ResultEnum.ACT_STOCKQTY_ERROR.getMessage());
        }
        return ResultBodyUtils.ok();
    }

    @PostMapping("updateSalesVolume")
    @Override
    public ResultBody updateSalesVolume(@RequestParam("goodsSpecificationId") long goodsSpecificationId,
                                        @RequestParam("salesVolume") int salesVolume) {
        Map<String,String> resultMap = goodsSpecificationService.updateSalesVolume(goodsSpecificationId,salesVolume);
        return ResultBodyUtils.ok();
    }
}
